import { FormSchema } from '@/components/Form';
import { useDictStore } from '@/store/modules/dict';
import { renderImage, renderOptionTag } from '@/utils';
import { defRangeShortcuts } from '@/utils/dateUtil';
import { cloneDeep } from 'lodash-es';
import { ref } from 'vue';

const dict = useDictStore();

export class State {
  public id = 0; // id
  public uid = ''; // 订单UUID
  public traceId = ''; // 交易ID
  public payMethod = null; // 支付方式
  public status = 0; // 订单状态
  public userId = 0; // 用户ID
  public storeId = 0; // 商家ID
  public orderType = null; // 订单类型
  public productId = 0; // 商品ID
  public productName = ''; // 商品名称
  public productMainImage = []; // 商品主图
  public qty = 0; // 购买数量
  public usePoints = null; // 使用积分
  public useAmount = null; // 使用金额(RMB)
  public expressInfoExpressCompany = null; // 物流公司
  public expressId = ''; // 快递单号
  public createdAt = ''; // 创建时间
  public updatedAt = ''; // 更新时间
  public payTime = ''; // 付款时间
  public confirmTime = ''; // 订单确认时间
  public endTime = ''; // 结束时间
  public archiveTime = ''; // 归档时间
  public expressInfoId = 0; // 快递信息
  public isConfirm = false;

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  // uid: {
  //   required: true,
  //   trigger: ['blur', 'input'],
  //   type: 'string',
  //   message: '请输入订单UUID',
  // },
  // payMethod: {
  //   required: true,
  //   trigger: ['blur', 'input'],
  //   type: 'string',
  //   message: '请输入支付方式',
  // },
  // status: {
  //   required: true,
  //   trigger: ['blur', 'input'],
  //   type: 'string',
  //   message: '请输入订单状态',
  // },
  // userId: {
  //   required: true,
  //   trigger: ['blur', 'input'],
  //   type: 'number',
  //   message: '请输入用户ID',
  // },
  // storeId: {
  //   required: true,
  //   trigger: ['blur', 'input'],
  //   type: 'number',
  //   message: '请输入商家ID',
  // },
  // orderType: {
  //   required: true,
  //   trigger: ['blur', 'input'],
  //   type: 'string',
  //   message: '请输入订单类型',
  // },
  // productId: {
  //   required: true,
  //   trigger: ['blur', 'input'],
  //   type: 'number',
  //   message: '请输入商品ID',
  // },
  // qty: {
  //   required: true,
  //   trigger: ['blur', 'input'],
  //   type: 'number',
  //   message: '请输入购买数量',
  // },
  // usePoints: {
  //   required: true,
  //   trigger: ['blur', 'input'],
  //   type: 'number',
  //   message: '请输入使用积分',
  // },
  // useAmount: {
  //   required: true,
  //   trigger: ['blur', 'input'],
  //   type: 'number',
  //   validator: validate.amount,
  // },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  // 单选
  // {
  //   field: 'status',
  //   component: 'NRadioGroup',
  //   label: '   ',
  //   componentProps: {
  //      options: [
  //         {
  //           label: '待发货',
  //           value: '待发货',
  //         },
  //       ],
  //     onUpdateValue: (e: any) => {
  //       console.log(e);
  //     },
  //   },
  // },
  {
    field: 'adminMemberMobile',
    component: 'NInput',
    label: '手机号码',
    componentProps: {
      placeholder: '请输入手机号码',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
    {
    field: 'orderType',
    component: 'NSelect',
    label: '订单类型',
    defaultValue: null,
    componentProps: {
      placeholder: '请选择订单类型',
      options: dict.getOption('orderType'),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'status',
    component: 'NSelect',
    label: '订单状态',
    defaultValue: null,
    componentProps: {
      placeholder: '请选择订单状态',
      options: dict.getOption('orderStatus'),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'uid',
    component: 'NInput',
    label: '订单UUID',
    componentProps: {
      placeholder: '请输入订单UUID',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'traceId',
    component: 'NInput',
    label: '交易ID',
    componentProps: {
      placeholder: '请输入交易ID',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'storeId',
    component: 'NInputNumber',
    label: '商家ID',
    componentProps: {
      placeholder: '请输入商家ID',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'productId',
    component: 'NInputNumber',
    label: '商品ID',
    componentProps: {
      placeholder: '请输入商品ID',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'expressId',
    component: 'NInput',
    label: '快递单号',
    componentProps: {
      placeholder: '请输入快递单号',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'createdAt',
    component: 'NDatePicker',
    label: '创建时间',
    componentProps: {
      type: 'datetimerange',
      clearable: true,
      shortcuts: defRangeShortcuts(),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'archiveTime',
    component: 'NDatePicker',
    label: '归档时间',
    componentProps: {
      type: 'datetimerange',
      clearable: true,
      shortcuts: defRangeShortcuts(),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: 'ID',
    key: 'id',
    align: 'left',
    width: 60,
  },
  {
    title: '商品主图',
    key: 'productMainImage',
    align: 'center',
    width: 80,
    render(row: State) {
      return renderImage(row.productMainImage? row.productMainImage[0] : "")
    },
  },
  {
    title: '商品名称',
    key: 'productName',
    align: 'left',
    width: 280,
  }, 

  // {
  //   title: '门店名称',
  //   key: 'storeName',
  //   align: 'left',
  //   width: -1,
  // },

  {
    title: '购买数量',
    key: 'qty',
    align: 'center',
    width: -1,
  },
  {
    title: '使用积分',
    key: 'usePoints',
    align: 'center',
    width: -1,
  },
  {
    title: '使用金额(元)',
    key: 'useAmount',
    align: 'center',
    width: 120,
  },
  {
    title: '支付方式',
    key: 'payMethod',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('payType', row.payMethod);
    },
  },
  {
    title: '订单状态',
    key: 'status',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('orderStatus', row.status);
    },
  },
  {
    title: '订单类型',
    key: 'orderType',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('orderType', row.orderType);
    },
  },
  {
    title: '订单UUID',
    key: 'uid',
    align: 'left',
    width: 220,
  },
  {
    title: '交易ID',
    key: 'traceId',
    align: 'left',
    width: 250,
  },
  // {
  //   title: '物流公司',
  //   key: 'expressCompany',
  //   align: 'left',
  //   width: -1,
  //   render(row: State) {
  //     return renderOptionTag('ExpressOptions', row.expressCompany);
  //   },
  // },
  {
    title: '创建时间',
    key: 'createdAt',
    align: 'left',
    width: 180,
  },
  // {
  //   title: '结束时间',
  //   key: 'endTime',
  //   align: 'left',
  //   width: 180,
  // },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['payType', 'orderStatus', 'orderType', 'ExpressOptions']);
}
